package llama.view.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import llama.core.Contact;
import llama.ctrl.AssignTagsCtrl;
import llama.ctrl.ControllerFactory;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class ContactInformationDialog extends JDialog implements ActionListener, ItemListener {

	private final JPanel contentPanel = new JPanel();
	
	private Contact contact;
	
	private JTextField textFirstName;
	private JTextField textLastName;
	private JTextField textEmail;
	private JLabel lblFailedToMail;
	private JComboBox comboBoxTags;
	private JButton btnApplyTag;
	
	Desktop desktop;

	/**
	 * Create the dialog.
	 */
	public ContactInformationDialog(final Contact contact) {
		this.contact = contact;
		
		textEmail = new JTextField(this.contact.getUser().getEmail());
		textFirstName = new JTextField(this.contact.getUser().getFirstName());
		textLastName = new JTextField(this.contact.getUser().getLastName());
		
		
		btnApplyTag = new JButton("Apply");
		btnApplyTag.setActionCommand("Apply tag");
		btnApplyTag.addActionListener(this);
		
		comboBoxTags = new JComboBox(llama.constants.DefaultTags.TAGS);
		comboBoxTags.setFont(new Font("Verdana", Font.PLAIN, 13));
		comboBoxTags.setSelectedItem(contact.getTag());
		comboBoxTags.addItemListener(this);
		updateTagAppliable();
		
		setTitle("Contact Information - "+this.contact.getUser().getFullName());
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		{
			JLabel staticLbl3 = new JLabel("Email:");
			staticLbl3.setFont(new Font("Tahoma", Font.PLAIN, 15));
			staticLbl3.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(staticLbl3, "6, 2");
		}
		{
			textEmail.setFont(new Font("Verdana", Font.PLAIN, 13));
			textEmail.setEditable(false);
			contentPanel.add(textEmail, "8, 2, fill, default");
		}
		{
			JButton btnSendEmail = new JButton("Send email");
			btnSendEmail.setActionCommand("Send email");
			btnSendEmail.addActionListener(this);
			contentPanel.add(btnSendEmail, "10, 2");
		}
		{
			JLabel staticLbl1 = new JLabel("First name:");
			staticLbl1.setFont(new Font("Tahoma", Font.PLAIN, 15));
			staticLbl1.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(staticLbl1, "6, 4");
		}
		{
			textFirstName.setFont(new Font("Verdana", Font.PLAIN, 13));
			textFirstName.setEditable(false);
			contentPanel.add(textFirstName, "8, 4, 3, 1, fill, default");
		}
		{
			JLabel staticLbl2 = new JLabel("Last name:");
			staticLbl2.setFont(new Font("Tahoma", Font.PLAIN, 15));
			staticLbl2.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(staticLbl2, "6, 6");
		}
		{
			textLastName.setFont(new Font("Verdana", Font.PLAIN, 13));
			textLastName.setEditable(false);
			contentPanel.add(textLastName, "8, 6, 3, 1, fill, default");
		}
		{
			JButton btnCopyFullName = new JButton("Copy Full Name");
			btnCopyFullName.setActionCommand("Copy name");
			btnCopyFullName.addActionListener(this);
			contentPanel.add(btnCopyFullName, "8, 8, 3, 1, right, default");
		}
		{
			JLabel label = new JLabel(" ");
			contentPanel.add(label, "6, 10");
		}
		{
			JLabel staticLbl4 = new JLabel("Tagged as:");
			staticLbl4.setHorizontalAlignment(SwingConstants.RIGHT);
			staticLbl4.setFont(new Font("Tahoma", Font.PLAIN, 15));
			contentPanel.add(staticLbl4, "6, 12, right, default");
		}
		{
			contentPanel.add(comboBoxTags, "8, 12, fill, default");
			contentPanel.add(btnApplyTag, "10, 12");

		}
		{
			JLabel lblFiller = new JLabel(" ");
			contentPanel.add(lblFiller, "6, 16");
		}
		{
			lblFailedToMail = new JLabel("Failed to start your mail application");
			lblFailedToMail.setFont(new Font("Arial", Font.PLAIN, 14));
			lblFailedToMail.setForeground(Color.RED);
			lblFailedToMail.setVisible(false);
			contentPanel.add(lblFailedToMail, "8, 16, 3, 1, right, default");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelButton = new JButton("Close");
				cancelButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
				cancelButton.addActionListener(this);
				cancelButton.setActionCommand("Close");
				buttonPane.add(cancelButton);
			}
		}
		
		setVisible(true);
	}
	
	private void applyTag() {
		if(comboBoxTags != null && comboBoxTags.getSelectedItem() instanceof String) {
			AssignTagsCtrl assignTagsCtrl = (AssignTagsCtrl) ControllerFactory.getController(AssignTagsCtrl.class);
			assignTagsCtrl.assignTag(contact.getUser(), (String) comboBoxTags.getSelectedItem());
			assignTagsCtrl.execute();	
		}
	}
	
	private void copyNameToClipboard() {
		String text = contact.getUser().getFullName();
		
	    Toolkit toolkit = Toolkit.getDefaultToolkit();
	    Clipboard clipboard = toolkit.getSystemClipboard();
	    StringSelection stringSel = new StringSelection(text);

	   clipboard.setContents(stringSel, null);
	}
	
	private void sendMail() {
		try {
			URI uri = new URI("mailto:"+contact.getUser().getEmail());
			Desktop.getDesktop().mail(uri);
			lblFailedToMail.setVisible(false);
		} catch (URISyntaxException e1) {
			System.out.println("Trying to launch mail-application with incorrect URI");
			e1.printStackTrace();
			lblFailedToMail.setVisible(true);
		} catch (IOException e2) {
			System.out.println("Failed to launch mail application.");
			lblFailedToMail.setVisible(true);
		}
	}
	
	private void updateTagAppliable() {
		if(comboBoxTags != null && comboBoxTags.getSelectedItem() instanceof String) {
			if(((String) comboBoxTags.getSelectedItem()).equals(contact.getTag())) {
				btnApplyTag.setEnabled(false);
			} else {
				btnApplyTag.setEnabled(true);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() != null) {
			if(e.getActionCommand().equals("Close")){
				this.dispose();
			} else if(e.getActionCommand().equals("Send email")){
				sendMail();
			} else if(e.getActionCommand().equals("Apply tag")) {
				applyTag();
			} else if (e.getActionCommand().equals("Copy name")) {
				copyNameToClipboard();
			}
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		updateTagAppliable();
	}

}
